package com.sansan.ultrasound_handpad.fragment.scan;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qmuiteam.qmui.util.QMUIKeyboardHelper;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.sansan.ultrasound_handpad.R;
import com.sansan.ultrasound_handpad.base.BaseFragment;
//import com.sansan.ultrasound_handpad.fragment.home.HomeFragment;
import com.sansan.ultrasound_handpad.fragment.process.ProcessFragment;
import com.sansan.ultrasound_handpad.manager.QDDataManager;
import com.sansan.ultrasound_handpad.model.QDItemDescription;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author ByLinYus
 * 2020-06-14 12:41
 * 扫描二维码的界面
 */
public class ScanFragment extends BaseFragment {
    private final static String TAG = ScanFragment.class.getSimpleName();

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;
    @BindView(R.id.scan_resultEt)
    EditText mScanResultEt;

    private QMUITipDialog tipDialog = null;

    /*接收扫码器传回来的扫码数据*/
    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mScanResultEt != null) {
                mScanResultEt.setText("");
            }
            /*扫描数据*/
            byte[] scanData = intent.getByteArrayExtra("barocode");
            /*数据长度*/
            int dataLen = intent.getIntExtra("length", 0);
            /*数据类型*/
            byte dataTyp = intent.getByteExtra("barcodeType", (byte)0);
            /*条码字符串*/
            String barcodeStr = new String(scanData, 0, dataLen);
            if (mScanResultEt != null) {
                mScanResultEt.setText(barcodeStr);
            }

            if (!TextUtils.isEmpty(barcodeStr)) {
                if (tipDialog != null) {
                    tipDialog.cancel();
                    tipDialog = null;
                }
                tipDialog = new QMUITipDialog.Builder(getContext())
                        .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                        .setTipWord("正在查询程序列表,请稍后......")
                        .create();
                tipDialog.show();

                uiHandler.sendEmptyMessageDelayed(0, 3 * 1000);
            }
        }
    };
    /*测试*/
    private Handler uiHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (tipDialog != null) {
                tipDialog.cancel();
                tipDialog = null;
            }
            try {
                startFragmentAndDestroyCurrent(ProcessFragment.class.newInstance());
            } catch (Exception e) {
                Toast.makeText(getContext(), "进入界面失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

        }
    };

    /*初始化标题栏*/
    private void initTopBar() {
        mTopBar.addLeftBackImageButton().setOnClickListener(v -> popBackStack());
        mTopBar.setTitle("扫描");
    }

    @Override
    protected View onCreateView() {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_scan, null);
        ButterKnife.bind(this, rootView);
        /*标题栏*/
        initTopBar();

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getActivity() == null) {
            return;
        }
        /*注册扫描器广播*/
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("urovo.rcv.message");
        getActivity().registerReceiver(this.mScanReceiver, intentFilter);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (getActivity() == null) {
            return;
        }
        /*注销扫描器广播*/
        getActivity().unregisterReceiver(this.mScanReceiver);
    }

    //    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == 82) {
////            openOptionsMenu();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    protected void popBackStack() {
        super.popBackStack();
        /*返回隐藏键盘*/
        QMUIKeyboardHelper.hideKeyboard(mScanResultEt);
    }
}
